﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpCart.BusinessObjects
{
    public class UserType
    {
        #region Declarations
        Data.SharpDb Db;
        Data.UserType DataUserType;
        bool IsNew;
        #endregion

        #region Properties
        public string Type
        {
            get
            {
                return DataUserType.Type;
            }
            set
            {
                DataUserType.Type = value;
            }
        }
        /// <summary>
        /// Gets a list of users that have this user type.
        /// </summary>
        public List<User> Users
        {
            get
            {
                if (!DataUserType.User.IsLoaded)
                {
                    DataUserType.User.Load();
                }
                List<User> Result = new List<User>();
                foreach (Data.User User in DataUserType.User)
                {
                    Result.Add(new User(User, Db));
                }
                return Result;
            }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Use this constructor to create new user types.
        /// </summary>
        public UserType()
        {
            Db = new SharpCart.Data.SharpDb();
            DataUserType = new SharpCart.Data.UserType();
            IsNew = true;
        }
        /// <summary>
        /// Use this constructor to load existing types.
        /// </summary>
        /// <param name="Type">The type to load</param>
        public UserType(string Type)
        {
            Db = new SharpCart.Data.SharpDb();
            DataUserType = Db.UserType.First(U => U.Type == Type);
            IsNew = false;
        }
        /// <summary>
        /// Use this constructor when you have a data object.
        /// </summary>
        /// <param name="DataUserType">The usertype data object</param>
        /// <param name="Db">The database context.</param>
        internal UserType(Data.UserType DataUserType, Data.SharpDb Db)
        {
            this.DataUserType = DataUserType;
            this.Db = Db;
            IsNew = false;
        }
        #endregion

        #region Instance Methods
        /// <summary>
        /// Save the user type to the database.
        /// </summary>
        public void Save()
        {
            if (IsNew)
            {
                Db.AddToUserType(DataUserType);
            }
            Db.SaveChanges();
            IsNew = false;

        }
        /// <summary>
        /// Delete the usertype from the database.
        /// </summary>
        public void Delete()
        {
            Db.DeleteObject(DataUserType);
            Db.SaveChanges();
        }

        #endregion

        #region Static Methods
        /// <summary>
        /// Get a list of all usertypes in the database.
        /// </summary>
        /// <returns>A list of all usertypes in the database.</returns>
        public static List<UserType> GetUserTypes()
        {
            List<UserType> Result = new List<UserType>();
            Data.SharpDb Db = new SharpCart.Data.SharpDb();
            var TypeList = from U in Db.UserType select U;
            foreach(Data.UserType DataUserType in TypeList)
            {
                Result.Add(new UserType(DataUserType, Db));
            }
            return Result;
        }
        #endregion
    }
}
